Centos7 编译安装Nginx

前言

nginx以高效的linux网络模型,epoll,event作为网络IO模型,kqueue,在高并发网站情况下,nginx能够轻松支持5w+的并发流量,并且消耗的服务器内存,cpu等资源,也是很低的,运行起来非常稳定。


一、编译安装nginx

在Linux系统中,软件的安装方式有两种:

  • 包管理安装
  • 编译安装

编译安装是指用户自己下载软件源代码,然后自己编译、配置、安装的安装方式。编译安装的优点是可以自定义编译选项,可以实现对软件的个性化定制,而缺点是安装过程相对复杂,需要手动编译、配置、安装,并且需要自己处理依赖关系。Nginx是一款高性能的Web服务器,可以作为反向代理服务器或负载均衡服务器使用。在Linux系统中,可以通过包管理器安装Nginx,也可以使用编译安装的方式安装Nginx。下面是关于编译安装Nginx的相关信息。

二、编译安装过程

1.操作系统的选择,centos7

代码如下:

1
2
3
4
[root@hmiking ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)
[root@hmiking ~]# uname -a
Linux hmiking 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
1.1关闭防火墙、selinux

代码如下:

1
2
3
4
5
[root@hmiking ~]# systemctl disable --now firewalld
[root@hmiking ~]# setenforce 0
setenforce: SELinux is disabled
[root@hmiking ~]# getenforce
Disabled

2.安装编译开发环境

代码如下:

1
[root@hmiking ~]# yum install -y gcc gcc-c++ autoconf automake make
2.1安装nginx所需的一些第三方系统库的支持

代码如下:

1
[root@hmiking ~]# yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel wget httpd-tools vim -y

3.编译安装nginx

3.1下载nginx源代码

代码如下:

1
[root@hmiking ~]# wget http://nginx.org/download/nginx-1.17.10.tar.gz
3.2解压缩nginx包,并进入该目录

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@hmiking ~]# tar xf nginx-1.17.10.tar.gz
[root@hmiking ~]# cd nginx-1.17.10
[root@hmiking nginx-1.17.10]# ll #查看目录下有哪些内容
total 760
drwxr-xr-x 6 mysql mysql 326 Mar 6 23:39 auto # 检测系统模块依赖信息
-rw-r--r-- 1 mysql mysql 302754 Apr 14 2020 CHANGES # 存放nginx的变化记录日志
-rw-r--r-- 1 mysql mysql 462076 Apr 14 2020 CHANGES.ru
drwxr-xr-x 2 mysql mysql 168 Mar 6 23:39 conf # 存放nginx主配置文件的目录
-rwxr-xr-x 1 mysql mysql 2502 Apr 14 2020 configure # 可执行的脚本,用于编译文件的定制脚本
drwxr-xr-x 4 mysql mysql 72 Mar 6 23:39 contrib # 提供了vim插件,让配置文件颜色区分,更友好
drwxr-xr-x 2 mysql mysql 40 Mar 6 23:39 html # 存放了标准的html页面文件
-rw-r--r-- 1 mysql mysql 1397 Apr 14 2020 LICENSE
drwxr-xr-x 2 mysql mysql 21 Mar 6 23:39 man
-rw-r--r-- 1 mysql mysql 49 Apr 14 2020 README
drwxr-xr-x 9 mysql mysql 91 Mar 6 23:39 src # 存放了nginx源代码的目录
3.3开始编译安装

代码如下:

1
2
3
4
5
6
7
8
9
# 进入软件源代码目录,执行编译脚本文件,如指定安装路径,以及开启额外功能等
[root@hmiking nginx-1.17.10]# mkdir /usr/local/nginx
[root@hmiking nginx-1.17.10]# ./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio && make && make install
3.4查看安装后的nginx目录

代码如下:

1
2
3
4
5
6
7
8
[root@hmiking nginx-1.17.10]# cd /usr/local/nginx/
[root@hmiking nginx]# ls
conf html logs sbin

# conf 存放nginx的配置文件,如 nginx.conf
# html 存放nginx的网页根目录文件,存放站点的静态文件数据
# logs 存放nginx的各种日志目录
# sbin 存放该软件的可执行命令

4.启动并访问nginx

4.1将 nginx 添加到全局变量中

代码如下:

1
2
3
4
5
# 可以通过软链接到/usr/local/sbin/ 目录下
[root@hmiking nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

# 此时可以快捷的使用nginx各种指令
nginx # 首次直接输入nginx,表示启动该进程
4.2检查nginx的编译安装信息

代码如下:

1
2
3
4
5
6
[root@hmiking nginx]# nginx -V
nginx version: nginx/1.17.10
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
4.3启动nginx

代码如下:

1
2
3
4
5
6
7
# 启动nginx
[root@hmiking ~]# nginx

# 查看nginx进程
[root@hmiking ~]# ps -ef | grep nginx | grep -v grep
root 27314 1 0 01:28 ? 00:00:00 nginx: master process nginx
nobody 27315 27314 0 01:28 ? 00:00:00 nginx: worker process
4.4通过命令行终端访问nginx

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@hmiking ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4.5通过浏览器访问nginx

在这里插入图片描述

欢迎关注我的CSDN个人博客知乎